package kugou

import (
	"api-go/cache"
	"api-go/serializer"
	kuwo "api-go/service/v1/kuwo"
	migu "api-go/service/v1/migu"
	wyy "api-go/service/v1/wyy"
	"api-go/util"
	"encoding/json"
	"fmt"
	"strconv"
	"time"

	"github.com/go-redis/redis/v8"
)

// 歌曲所需信息
type Song struct {
	Ids    string `form:"id"`
	Hash   string `form:"hash"`
	Limit  string `form:"limit"`
	Offset string `form:"offset"`
	Br     string `form:"br"`
	Type   string `form:"type"` //vip播放源，1酷我 2咪咕 3网易云
}

//获取歌曲详细信息(包括歌词和播放地址)
func (service *Song) SongInfo() *serializer.Response {

	if val := service.Ids; val != "" {
		service.Ids = val
	} else {
		service.Ids = "53456794"
	}

	if val := service.Hash; val != "" {
		service.Hash = val
	} else {
		service.Hash = "1A83EAEDDE7666CE81300D64B37402D9"
	}

	if val := service.Br; val != "" {
		service.Br = val
	} else {
		service.Br = "1"
	}

	if val := service.Type; val != "" {
		service.Type = val
	} else {
		service.Type = "1"
	}

	url := "https://wwwapi.kugou.com/yy/index.php?r=play/getdata&hash=" + service.Hash + "&album_id=" + service.Ids

	res, err := util.KugouGet(url)
	if err != nil {
		return err
	}

	out := map[string]interface{}{}

	ress := res.(map[string]interface{})["data"].(map[string]interface{})
	out["id"] = ress["album_id"]
	out["hash"] = ress["hash"]
	out["album"] = ress["album_name"]
	out["name"] = ress["song_name"]
	out["singer"] = ress["author_name"]
	out["picUrl"] = ress["img"]
	out["platform"] = "kg"
	out["lyric"] = ress["lyrics"]

	if strconv.FormatFloat(ress["privilege"].(float64), 'f', -1, 64) != "10" && service.Br == "1" {
		out["url"] = ress["play_url"]
	} else {
		if service.Type == "3" {
			out["url"] = wyy.UsingWyySong(out["name"].(string), out["singer"].(string), service.Br)
		} else if service.Type == "2" {
			out["url"] = migu.UsingMiguSong(out["name"].(string), out["singer"].(string), service.Br)
		} else {
			out["url"] = kuwo.UsingKwSong(out["name"].(string), out["singer"].(string), service.Br)
		}
	}

	return serializer.OkResponse(out)
}

//获取最新歌曲
func (service *Song) SongNew() *serializer.Response {

	if val := service.Ids; val != "" {
		service.Ids = val
	} else {
		//1华语  2欧美  3日韩
		service.Ids = "1"
	}

	if val := service.Limit; val != "" {
		service.Limit = val
	} else {
		service.Limit = "20"
	}

	if val := service.Offset; val != "" {
		service.Offset = val
	} else {
		service.Offset = "1"
	}
	//定义redis key和判断redis是否有该值
	redisKey := "kugou-song-new-" + service.Ids + "-" + service.Limit + "-" + service.Offset
	redisResult, err := cache.RedisClient.Get(ctx, redisKey).Result()
	//若redis没有该值则执行获取
	if err == redis.Nil {

		url := "http://mobilecdnbj.kugou.com/api/v3/rank/newsong?version=9108&plat=0&with_cover=1&pagesize=" + service.Limit + "&type=" + service.Ids + "&area_code=1&page=" + service.Offset + "&with_res_tag=1"
		res, err := util.KugouGetS(url)
		if err != nil {
			return err
		}

		//将结果转为Json数据
		b, e := json.Marshal(res)
		if e != nil {
			fmt.Println(err)
		}
		//转为string存入redis
		e = cache.RedisClient.Set(ctx, redisKey, string(b), 24*time.Hour).Err()
		if e != nil {
			fmt.Println("err1", err)
			return serializer.ErrorResponse(serializer.RedisError)
		}

		return serializer.OkResponse(res)
	} else if err != nil {
		fmt.Println("err2", err)
		return serializer.ErrorResponse(serializer.RedisError)
	} else {
		//将从redis获取的结果（string类型）转为map[string]inerface{}
		bodyMapData := map[string]interface{}{}
		err = json.Unmarshal([]byte(redisResult), &bodyMapData)
		if err != nil {
			fmt.Println("err3", err)
			return serializer.ErrorResponse(serializer.RedisError)
		}
		return serializer.OkResponse(bodyMapData)
	}

}
